import java.io.*;

public class GestorParking {
	
	private static Vehiculo[][] arrayVehiculos = new Vehiculo [3][20];
	
			
	
	public static void sacarVehiculo(String matricula) {
		boolean sacado = false;
		for (int i = 0; i < arrayVehiculos.length; i++) {
			for (int j = 0; j < arrayVehiculos[i].length; j++) {
				if (arrayVehiculos[i][j] != null) {
					if (arrayVehiculos[i][j].getMatricula().equals(matricula)) {
						guardarLog(arrayVehiculos[i][j].toString() + ",salida");
						arrayVehiculos[i][j] = null;
						sacado = true;
						break;
					}	
				}
			}
			if (sacado) {
				break;
			}
		}
	}
	
	public static Vehiculo obtenerVehiculo(int x, int y) {		
		return arrayVehiculos[x][y];
	}
	
	public static void meterVehiculo(int x, int y, Vehiculo vehiculo) {
		arrayVehiculos[x][y] = vehiculo;
		guardarLog(vehiculo.toString()+",entrada");		
	}
	
	public static int[] buscarPlaza(boolean minusvalido) {
		int[] plaza = new int [2];
		boolean encontrada = false;
		if (minusvalido) {
			for (int i = 0; i < arrayVehiculos.length; i++) {
				for (int j = 0; j < 2; j++) {
					if (arrayVehiculos[i][j] == null) {
						encontrada = true;
						plaza[0] = i;
						plaza[1] = j;
						break;
					}
				}
				if (encontrada) {
					break;
				}
			}
			if (!encontrada) {
				buscarPlaza(false);
			}
		}
		else {
			for (int i = 0; i < arrayVehiculos.length; i++) {
				for (int j = 2; j < arrayVehiculos[i].length; j++) {
					if (arrayVehiculos[i][j] == null) {						
						encontrada = true;
						plaza[0] = i;
						plaza[1] = j;
						break;
					}
				}
				if (encontrada) {
					break;
				}
			}
		}
		if (!encontrada) {
			plaza[0] = -1;
			plaza[1] = -1;
		}
		return plaza;
	}
	
	public static void guardarLog(String linea) {
		try {
			BufferedWriter fS=new BufferedWriter(new FileWriter("src/log.txt",true));			
			fS.write(linea);	
			fS.newLine();			
			fS.close();
		}
		catch (IOException ioE) {
			System.out.println(ioE.getMessage());
		}
	}
}
